package jkmg

import (
    "labix.org/v2/mgo"
    "fmt"
    "strconv"
)

func JKConnMongo(host string, port int) (s *mgo.Session, err error) {
    url := "mongodb://"+host+":"+strconv.Itoa(port)
    s, err = mgo.Dial(url)
    if err != nil {
        fmt.Printf("connect [%s] error\n", url)
        return nil, err
    }
    return s, err
}

func JKGetDB(host string, port int, database string) (db *mgo.Database, err error) {
    s, err := JKConnMongo(host, port)
    if err == nil {
        db = s.DB(database)
        return db, err
    }
    return nil, err
}

func JKGetCollection(host string, port int, database string, coll string) (c *mgo.Collection, err error) {
    db, err := JKGetDB(host, port, database)
    if err == nil {
        c = db.C(coll)
        return c, err
    }
    return nil, err
}


